package com.qf.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
public class HelloListener {
    @Autowired
    private RedisTemplate redisTemplate;
    @RabbitListener(queues = "testQueue1")
    public void hello(String str, Channel channel, Message message){


        try {
            String correlation =(String) message.getMessageProperties().getHeaders().get("spring_returned_message_correlation");
            if (redisTemplate.opsForValue().setIfAbsent(correlation, "0", 3, TimeUnit.SECONDS)){
                System.out.println("=========测试1========"+str);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            }else {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            }

        } catch (Exception e) {
            e.printStackTrace();
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}
